package com.ranyk.www.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ranyk.www.base.BasePageDTO;
import com.ranyk.www.entity.AdminUser;
import com.ranyk.www.entity.Organization;
import com.ranyk.www.enums.StatusEnum;
import com.ranyk.www.mapper.AdminUserMapper;
import com.ranyk.www.mapper.OrganizationMapper;
import com.ranyk.www.pojo.vo.OrganizationVO;
import com.ranyk.www.service.IOrganizationService;
import com.ranyk.www.support.page.PageInfo;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * CLASS_NAME: OrganizationServiceImpl.java <br/>
 *
 * @author ranyk           <br/>
 * @version V1.0           <br/>
 * @decription: 组织表 业务逻辑层接口实现  <br/>
 * @date: 2022-04-03 <br/>
 */
@Service
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements IOrganizationService {


    private OrganizationMapper organizationMapper;

    private AdminUserMapper adminUserMapper;

    @Autowired
    public void setOrganizationMapper(OrganizationMapper organizationMapper) {
        this.organizationMapper = organizationMapper;
    }

    @Autowired
    public void setUserMapper(AdminUserMapper adminUserMapper) {
        this.adminUserMapper = adminUserMapper;
    }

    /**
     * 创建组织
     *
     * @param organization 组织信息
     * @param userName     操作人
     * @return 返回新增数据结果
     */
    @Override
    public Integer insertOrganization(Organization organization, String userName) {
        organization.setCreateBy(userName);
        organization.setCreateTime(new Date());
        organization.setUpdateBy(userName);
        organization.setUpdateTime(new Date());
        return organizationMapper.insert(organization);
    }

    @Override
    public List<Organization> listByStatus(Integer status) {
        Organization organization = new Organization();
        organization.setStatus(status);
        return organizationMapper.selectList(new QueryWrapper<>(organization));
    }

    @Override
    public BasePageDTO<Organization> listByPage(PageInfo pageInfo, String search) {
        Page<Organization> page = new Page<>(pageInfo.getCurrent(), pageInfo.getLimit());
        RowBounds rowBounds = new RowBounds(pageInfo.getCurrent(), pageInfo.getLimit());
        List<Organization> organizations = organizationMapper.listByPage(pageInfo, search, rowBounds);
        pageInfo.setTotal(Long.valueOf(page.getTotal()).intValue());
        return new BasePageDTO<>(pageInfo, organizations);
    }

    @Override
    public List<OrganizationVO> listOrganizationsDetail() {
        List<OrganizationVO> organizationVOList = new ArrayList<>();
        // 查询所有组织
        List<Organization> organizations = organizationMapper.selectList(new QueryWrapper<>());
        // 循环遍历组织,将用户信息添加该组织中
        for (Organization organization : organizations) {
            AdminUser user = new AdminUser();
            user.setOrganizationId(organization.getOrganizationId());
            List<AdminUser> users = adminUserMapper.selectList(new QueryWrapper<>(user));
            OrganizationVO organizationVO = new OrganizationVO();
            BeanUtils.copyProperties(organization, organizationVO);
            organizationVO.setUsers(users);
            organizationVO.setNumberUser(users.size());
            organizationVOList.add(organizationVO);
        }
        return organizationVOList;
    }

    @Override
    public Integer updateStatus(Long organizationId) {
        Organization organization = organizationMapper.selectById(organizationId);
        if (organization != null && StatusEnum.NORMAL.getStatus().equals(organization.getStatus())) {
            Organization updateOrganization = new Organization();
            updateOrganization.setOrganizationId(organization.getOrganizationId());
            updateOrganization.setStatus(StatusEnum.FREEZE.getStatus());
            return organizationMapper.updateById(updateOrganization);
        } else if (organization != null && StatusEnum.FREEZE.getStatus().equals(organization.getStatus())) {
            Organization updateOrganization = new Organization();
            updateOrganization.setOrganizationId(organization.getOrganizationId());
            updateOrganization.setStatus(StatusEnum.NORMAL.getStatus());
            return organizationMapper.updateById(updateOrganization);
        }
        return null;
    }

    @Override
    public Integer updateOrganization(Organization organization, String userName) {
        organization.setUpdateBy(userName);
        organization.setUpdateTime(new Date());
        return organizationMapper.updateById(organization);
    }

    @Override
    public Integer deleteByOrganizationId(Long organizationId) {
        // 重置管理员组织记录
        adminUserMapper.updateOrganization(organizationId);
        // 删除组织表中记录
        return organizationMapper.deleteById(organizationId);
    }

}
